package microcredit.gui.util;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import microcredit.model.util.ModelUtils;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.plaf.basic.CalendarHeaderHandler;
import org.jdesktop.swingx.plaf.basic.SpinningCalendarHeaderHandler;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

public class MDatePicker extends JXDatePicker {
	
	public MDatePicker() {
		setLocale(Locale.ENGLISH);
		setFormats(FormatUtilities.getDateFormat());
		getMonthView().setShowingWeekNumber(true);
		getEditor().addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				try {
			        try {
			        	Date previousDate = getDate();
			            ((JFormattedTextField)getEditor()).commitEdit();
			            
			            if (!ModelUtils.equals(previousDate, getDate())) {
			            	fireActionPerformed(COMMIT_KEY);
			            }
			        } catch (ParseException e2) {
			            // re-throw
			            throw e2;
			        } 
				} catch (ParseException e1) {
					cancelEdit();
				}
			}
		});
		
		final JFormattedTextField textField = getEditor();
		textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
		
//		final DocumentListener listener = (new DocumentListener() {
//			@Override
//			public void removeUpdate(DocumentEvent e) {
//				update();
//			}
//			
//			@Override
//			public void insertUpdate(DocumentEvent e) {
//				update();
//			}
//			
//			@Override
//			public void changedUpdate(DocumentEvent e) {
//				update();
//			}
//			private void update() {
//				if (textField.isValid()) {
//					EventQueue.invokeLater(new Runnable() {
//						public void run() {
//							try {
//								textField.commitEdit();
//							} catch (ParseException e) {
//							}
//						}
//					});
//				}
//			}
//		});
//		getEditor().addPropertyChangeListener("document", new PropertyChangeListener() {
//			@Override
//			public void propertyChange(PropertyChangeEvent evt) {
//				if (evt.getOldValue() != null) {
//					((Document)evt.getOldValue()).removeDocumentListener(listener);
//				}
//				if (evt.getNewValue() != null) {
//					((Document)evt.getNewValue()).addDocumentListener(listener);
//				}
//			}
//		});
//		getEditor().getDocument().addDocumentListener(listener);

	}
	
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
	} 
	
	public Date getDate() {
		return super.getDate();
	}
	
	public void setDate(Date date) {
		super.setDate(date);
	}
	
	public static void main(String[] args)
			throws UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(new NimbusLookAndFeel());
		UIManager.put(CalendarHeaderHandler.uiControllerID, SpinningCalendarHeaderHandler.class.getName());
		UIManager.put(SpinningCalendarHeaderHandler.FOCUSABLE_SPINNER_TEXT, Boolean.TRUE);
		UIManager.put(SpinningCalendarHeaderHandler.ARROWS_SURROUND_MONTH, Boolean.TRUE);
		UIManager.put("JXDatePicker.forceZoomable", Boolean.TRUE);
		MDatePicker p = new MDatePicker();
		p.setDate(new Timestamp(System.currentTimeMillis()));

		System.out.println(p.getDate());
		JFrame frame = new JFrame();
		frame.getContentPane().add(p);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	
}
